package top.laoshuzi.common.mvp.presenter;

import android.support.annotation.Nullable;

import java.util.concurrent.CopyOnWriteArrayList;

public class Presenter<View> implements PresenterLifecycle {

    @Nullable
    private View view;
    private CopyOnWriteArrayList<OnLifecycleListener> onLifecycleListeners = new CopyOnWriteArrayList<>();


    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }

    @Nullable
    public View getView() {
        return view;
    }


    public void create(View view) {
        this.view = view;
        for (OnLifecycleListener listener : onLifecycleListeners)
            listener.onCreate();
        onCreate();
    }


    public void destroy() {
        for (OnLifecycleListener listener : onLifecycleListeners)
            listener.onDestroy();
        onDestroy();
        view = null;
    }

    public void addOnLifecycleListener(OnLifecycleListener listener) {
        onLifecycleListeners.add(listener);
    }

    public void removeOnLifecycleListener(OnLifecycleListener listener) {
        onLifecycleListeners.remove(listener);
    }

    public interface OnLifecycleListener {

        void onCreate();

        void onDestroy();
    }

}
